import React from 'react'
// ===================================================================== Temp
import Table from '#cpt/temp/Table'
import TableControls from '#cpt/temp/TableControls'
import Controls from '#cpt/temp/Controls'
import Confirm from '#cpt/temp/Confirm'
import AddEditModal from '#cpt/temp/AddEditModal'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== antd
// ===================================================================== Lazy
const Box 		= $Lazy.load(()=>import('#tp/box/box'))
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== data
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		formData : [
			{ label: '结果转换名称',			name: 'conv_name',		full:true, 	required:true},
			{ label: '转换条件', 			names: ['conv_field','conv_condition','conv_val'], full:true, required:true, type:'conversion'},
			{ label: '关联结果内容',			name: 'result_info_id',			full:true, 	required:true, type:'select', loading:true, idStr:'uuid', nameStr:'result_content'},
			{ label: '关联建议解释',			name: 'result_sugg_id',			full:true, 	type:'select', loading:true, idStr:'uuid', nameStr:'sugg_content' },
		]
	}
	
	model = {}
	
	componentDidMount(){
		const { kind_id } = this.props
		
		this.model = this.submit = { kind_id }
		
		this.fetch()
		
		$cache.then( async f => {
			const resultData = await f.result_data
			//
			this.setState({ resultData })
		})
	}
	// 获取弹框下拉
	getModalSelect = async () => {
		const { formData } = this.state
		const param = {kind_id: this.submit.kind_id}
		
		$fn.setModalRows(formData, [ 'result_info_id', 'result_sugg_id' ], ()=>{
			$cache.then( async f => {
				// 关联建议解释
				$fn.setRow(formData, 'result_sugg_id', await f.getCache({ api:'report-result-suggestion/select', param, way:'post', refresh:true}))
				
				// 关联结果内容
				$fn.setRow(formData, 'result_info_id', await f.getCache({ api:'report-result-info/select', param, way:'post', refresh:true}))
				
				//
				this.setState({ formData })
			})
		})
	}
	// paging
	fetch = () => $fn.fetch.call(this,'result-conv-rule/index')
	
	// 表单
	cols = [
		{ title: '数据转换',				dataIndex: 'conv_field', 	render: text => $fn.getState(this.state.resultData, text)  },
		{ title: '结果转换名称', 		dataIndex: 'conv_name', 		width:200 },
		{ title: '转换符号', 			dataIndex: 'conv_condition', 	width:200},
		{ title: '转换值', 				dataIndex: 'conv_val', 			...$fn.table.num},
		{ title: '关联结果内容', 		dataIndex: 'result_content', 	width:200},
		{ title: '关联建议解释', 		dataIndex: 'sugg_content', 	width:200},
		TableControls.controls.call(this,{ width: 80 })
	]
	// 表格操作按钮
	table_controls = (text,{ uuid }) => {
		return [
			TableControls.edit.call(this,'result-conv-rule/info', { uuid }, {
				select: this.getModalSelect,
				disabled:this.props.disabled
			}),
		]
	}
	// 操作按钮
	controls = () => {
		const { disabled } = this.props
		return [
			Controls.add.call(this,{click:this.getModalSelect, code:false, disabled}),
			Confirm.call(this,'删除', 'result-conv-rule/del', { isKeys: true, ghost:true, disabled })
		]
	}
	render(){
		const title = '结果转换规则'
		return (
			<>
				<Box key={this.props.disabled} title={title} controls={this.controls()} className='mt5' contentClassName='pt10'>
					{/* 表格 */}
					{ Table.call(this, { wrapStyle: this.props.tableStyle, disabled:this.props.disabled }) }
				</Box>
				{/* 添加、编辑 */}
				{ AddEditModal.call(this,{ title, labelWidth:'s7', api: 'result-conv-rule/add', editApi: 'result-conv-rule/edit' }) }
			</>
		)
	}
}

export default Index